import api from '../request.js'

// 声明对象存储所有请求
let Obj = {}
// 获取预置的get请求
const keys = Object.keys(api.get)
const vals = Object.values(api.get)
// 遍历注册请求
for (let i = 0 ; i < keys.length ; i ++) {
	const key = keys[i]
	Obj[key] = (obj) => {
		return new Promise((resolve, reject)=>{
			try{
				uni.request({
					url: api.url + vals[i],
					data: obj ? obj: null,
					header: {
						// Authorization: `${uni.getStorageSync('token')}`
						Authorization: `Bearer ${uni.getStorageSync('token')}`
					},
					method: 'get',
					success(res){
						resolve(res)
						return
						const { data , statusCode } = res
						if (statusCode == 401) {
							uni.$emit('changeCurrent', {current: null})
						}else{
							resolve(data)
						}
					},
					fail(res) {
						reject(res)
					}
				})
			}catch(e){
				//TODO handle the exception
				reject(e)
			}
		})
	}
}
// 暴露请求
export default Obj

